package com.xiaoyu.lanling.feature.moment.viewholder.notice

import `in`.srain.cube.views.list.ViewHolderBase
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.emoji.widget.EmojiTextView
import com.xiaoyu.base.app.App
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.base.view.OnClickDebounceListener
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.moment.model.notice.CommentNoticeItem
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.view.UserAvatarDraweeView
import com.xiaoyu.lanling.view.text.UserNameTextView

class CommentNoticeTextItemViewHolder : ViewHolderBase<CommentNoticeItem>() {
    private lateinit var avatar: UserAvatarDraweeView
    private lateinit var name: UserNameTextView
    private lateinit var verify: ImageView
    private lateinit var comment: EmojiTextView
    private lateinit var time: TextView
    private lateinit var feedText: EmojiTextView

    private val onAvatarClickListener = object : OnClickDebounceListener() {
        override fun onClickDebounce(v: View) {
            val activity = App.getInstance().topActivity ?: return
            val itemData = v.getViewTag<CommentNoticeItem>() ?: return
            val user = itemData.user

            Router.instance.gotoUserActivity(activity, user)
        }
    }

    private val onItemClickListener = object : OnClickDebounceListener() {
        override fun onClickDebounce(v: View) {
            val activity = App.getInstance().topActivity ?: return
            val itemData = v.getViewTag<CommentNoticeItem>() ?: return
            Router.instance.gotoMomentDetailActivity(activity, itemData.feed.id)
        }
    }

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View {
        val rootView = layoutInflater.inflate(R.layout.comment_notice_text_item, parent, false)
        avatar = rootView.findViewById(R.id.avatar)
        name = rootView.findViewById(R.id.name)
        verify = rootView.findViewById(R.id.verify)
        comment = rootView.findViewById(R.id.comment)
        feedText = rootView.findViewById(R.id.feed_text)
        time = rootView.findViewById(R.id.time)
        avatar.setOnClickListener(onAvatarClickListener)
        rootView.setOnClickListener(onItemClickListener)
        return rootView
    }

    override fun showData(position: Int, itemData: CommentNoticeItem) {
        ImageViewUtil.loadImage(avatar, itemData.avatarLoadParam)
        name.setUser(itemData.user)
        verify.visibility = if (itemData.verified) View.VISIBLE else View.INVISIBLE
        feedText.text = itemData.feed.text
        comment.text = itemData.commentText
        time.text = itemData.timeDesc
        avatar.setViewTag(itemData)
        mCurrentView.setViewTag(itemData)
    }
}